package com.tanhua.sso.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * 对自动填充字段的处理
 */
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object created = getFieldValByName("created", metaObject);
        if (null==created){
            //如果字段为空，那么进行填充
            setFieldValByName("created",new Date(),metaObject);
        }
        Object updated = getFieldValByName("updated", metaObject);
            //如果字段为空，那么进行填充
        if (null==updated){
            setFieldValByName("updated",new Date(),metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object updated = getFieldValByName("updated", metaObject);
        if (null == updated) {
            //字段为空，可以进行填充
            setFieldValByName("updated", new Date(), metaObject);
        }
    }
}
